<?php

/*define('DISPLAY_ERRORS', true);
define('DISPLAY_EXCEPTIONS', true);

set_time_limit(0);
ini_set('memory_limit',-1);
ini_set('max_execution_time',0);
ini_set('ignore_user_abort','On');
ini_set('display_errors', (DISPLAY_ERRORS)?(1):(0));
ini_set('display_startup_errors', (DISPLAY_ERRORS)?(1):(0));
error_reporting((DISPLAY_ERRORS)?(1):(0));
ignore_user_abort (true);
date_default_timezone_set('Europe/Bucharest');*/

/**
*
* SMTP4PHP :  PHP powerful tool for sending e-mails fast and easily.
*
* SMTP4PHP is a collection of PHP classes, dedicated for composing and sending 
* multipart/mixed email messages quickly and easily, with or without embedded 
* images and/or attachments.
*
* Copyright (c) 2011 - 2012, Raul IONESCU <ionescu.raul@gmail.com>, 
* Bucharest, ROMANIA
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @package      SMTP4PHP
* @author       Raul IONESCU <ionescu.raul@gmail.com>
* @copyright    Copyright (c) 2011 - 2012, Raul IONESCU.
* @license      http://www.opensource.org/licenses/mit-license.php The MIT License
* @version      2011, 14th release  
* @link         https://plus.google.com/u/0/109110210502120742267
* @access       public
*
* PHP versions 5.3 or greater
*/

/*

GMail - TLS encryption example 
==============================================
define('SMTP_SERVER','tls://smtp.gmail.com');
define('SMTP_SERVER_PORT',587);

GMail - SSL encryption example 
==============================================
define('SMTP_SERVER','ssl://smtp.gmail.com');
define('SMTP_SERVER_PORT',465);

Yahoo Mail - SSL encryption example
==============================================
define('SMTP_SERVER','ssl://smtp.mail.yahoo.com');
define('SMTP_SERVER_PORT',465);

Windows Live.com - TLS encryption example
==============================================
define('SMTP_SERVER','tls://smtp.live.com');
define('SMTP_SERVER_PORT',587);

*/

define('SMTP_SERVER','ssl://smtp.gmail.com');
define('SMTP_SERVER_PORT',465);

define('SMTP_USER','blackboxsurvey@gmail.com');
define('SMTP_PASSWORD','Moodle@123');

define('FROM_NAME','Blackbox Survey');
define('FROM_EMAIL',SMTP_USER);

/*///////////////////////////////////////////////////////////////////////////////////////*/
/*///////////////////////////////////////////////////////////////////////////////////////*/
/*///////////////////////////////////////////////////////////////////////////////////////*/

require_once('SMTP4PHP.php');

//require config nhat dinh phai dat o day, sau require_once('SMTP4PHP.php');
require_once('../../../config.php');//***
//***************************************

use SMTP4PHP\User;
/* 
//NOTE: Only if backward compatibility is really needed.
use SMTP4PHP\eMailUser; 
*/
use SMTP4PHP\eMail;
use SMTP4PHP\SMTP;

//get mail content
global $DB;
$n = $_GET["count"];
for($i = 1; $i <= $n; $i++)
{
    $mail = $_GET["mail".$i];
    $array = $pieces = explode("_", $mail);
    $sql = 'SELECT s.subject_name_vn, p.program_name, bb.date_start, bb.date_end, p.id as p_id, bb.guide_input, bb.guide_output, u.firstname, u.lastname, u.email
                FROM mdl_b_blackbox_survey AS bb
                JOIN mdl_program AS p ON bb.id_program = p.id
                JOIN mdl_subject AS s ON s.program_id = p.id
                JOIN mdl_teacher_subject AS ts ON ts.subject = s.id
                JOIN mdl_user AS u on u.id = ts.teacher
                where bb.id = '.$array[0].' and ts.teacher = '.$array[1].' and s.id = '.$array[2];
    $rs = $DB->get_record_sql($sql);
    
    
    $e = new eMail();
    $e->from = new User(FROM_NAME, FROM_EMAIL);
    //$e->to = new User(FROM_NAME, FROM_EMAIL);
    $e->to = new User("Receiver's name", $rs->email);
    $e->subject = 'Blackbox Survey Remind';
    /* 
    // EXAMPLE: add inline image example
    $e->htmlMessage = 'This is a HTML message!<br><img src="'.$e->addImage('./image.jpg').'" border="0">';
    $e->txtMessage = 'text content';
    */
    $e->htmlMessage = 'Dear Mr/Mrs.'.$rs->firstname.' '.$rs->lastname.'<br />'.
                        'You are require to do the questionaire of <br />'.
                        'Program: '.$rs->program_name.'<br />'.
                        'Subject: '.$rs->subject_name_vn.'<br />'.
                        'Publish date: '.date('Y-m-d H:i:s', $rs->date_start).'<br />'.
                        'Deadline: '.date('Y-m-d H:i:s', $rs->date_end);
    /*
    // EXAMPLE: add attachment example
    $e->addAttachment('Attachment.zip'); 
    */
    $smtp = new SMTP(SMTP_SERVER, SMTP_SERVER_PORT, SMTP_USER, SMTP_PASSWORD);
    /* NOTE: ALL emails are sent through the same connection, speeding up transmission. */
    try { $smtp->send($e); /* OR $smtp->send($e,$e2);*/ } 
    catch(Exception $e) { }
}
//var_dump($smtp->SMTPlog);
/*///////////////////////////////////////////////////////////////////////////////////////*/
/*///////////////////////////////////////////////////////////////////////////////////////*/
/*///////////////////////////////////////////////////////////////////////////////////////*/
?>
